package com.wxhandle.cleandemo.api.controller;

import com.wxhandle.cleandemo.application.dto.common.Result;
import com.wxhandle.cleandemo.application.dto.identity.SysRoleDto;
import com.wxhandle.cleandemo.application.dto.identity.SysRoleParam;
import com.wxhandle.cleandemo.core.domain.identity.SysRole;
import com.wxhandle.cleandemo.core.services.SysRoleService;

import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

@Api(tags="角色接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("api/role")
public class SysRoleController {

    private final SysRoleService roleService;
    private final ModelMapper modelMapper;
    
    @ApiOperation("分页查询角色")
    @GetMapping("page")
    public Page<SysRoleDto> getRolePage(SysRoleParam param, int page, int size) {
        return roleService.getPage(param, page, size).map(r->modelMapper.map(r,SysRoleDto.class));
    }

    @ApiOperation("新增角色")
    @PostMapping
    public SysRoleDto createRole(@RequestBody SysRoleDto role){
        SysRole result = roleService.insert(modelMapper.map(role,SysRole.class));
        return modelMapper.map(result, SysRoleDto.class);
    }

    @ApiOperation("更新角色")
    @PutMapping
    public SysRoleDto updateRole(@RequestBody SysRoleDto role){
        SysRole result = roleService.update(modelMapper.map(role, SysRole.class));
        return modelMapper.map(result, SysRoleDto.class);
    }

    @ApiOperation("删除角色")
    @DeleteMapping("{id}")
    public Result deleteRole(@PathVariable("id") Long id){
        roleService.deleteById(id);
        return Result.ofSuccess("Successed");
    }

    @ApiOperation("更新角色权限")
    @PutMapping(value = "{id}/permission",produces = "application/json;charset=UTF-8")
    public Result updateRolePermission(@PathVariable("id") Long id,@RequestBody String[] perms){
        roleService.updatePermission(id, perms);
        return Result.ofSuccess("Successed");
    }
}
